import React, { useEffect, useState } from "react";
import { View } from "@tarojs/components";
import { Button } from "@nutui/nutui-react-taro";
import BaseEchart from "@/components/BaseEchart";
import { EChartOption } from "taro-react-echarts";
import "./index.scss";
import SupRequest from "@/utils/api";
import Taro from "@tarojs/taro";
import {
  PAGE_WEBVIEW,
  getRandomString,
  goToPage,
  urlStringify,
} from "@/utils/common";
import NoTokenInterceptor from "@/components/NoTokenInterceptor";
import { useGlobalData } from "@/store/index";

const default_option: EChartOption = {
  legend: {
    top: 50,
    left: "center",
    z: 100,
  },
  tooltip: {
    trigger: "axis",
    show: true,
    confine: true,
  },
  xAxis: {
    type: "category",
    data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  },
  yAxis: {
    type: "value",
  },
  series: [
    {
      data: [150, 230, 224, 218, 135, 147, 260],
      type: "line",
    },
  ],
};

function Index() {
  const [currentOption, setCurrentOption] = useState(default_option);

  const { isLogin } = useGlobalData();

  const getData = async () => {
    console.log("getData==>");
    const res = await SupRequest({
      url: `/apis/posts/findAll`,
      method: "GET",
      data: {},
      // notCarryAuthority: true,
      // responseType: "blob",
    } as any);
    console.log("getData res", res);
  };
  const toSync = async () => {
    console.log("toSync==>");
    const res = await SupRequest({
      url: `/adapter/sup-adapter/feishu/sync/openid`,
      method: "POST",
    });
    console.log("toSync res", res);
  };

  const toLogin = async () => {
    const app_id = "cli_a5164911833a900c";
    const redirect_uri = "http://192.168.182.39:10086/#/pages/index/index";
    let aa = `https://open.feishu.cn/open-apis/authen/v1/index`;
    const payload = {
      redirect_uri,
      app_id,
    };
    // https://open.feishu.cn/open-apis/authen/v1/index?redirect_uri=http%3A%2F%2F192.168.182.39%3A10086%2F%23%2Fpages%2Findex%2Findex&app_id=cli_a5164911833a900c
    let viewUrl = urlStringify(aa, payload);
    let targetUrl = urlStringify(PAGE_WEBVIEW, { url: viewUrl });
    console.log("viewUrl", viewUrl);
    console.log("targetUrl", targetUrl);
    Taro.navigateTo({
      url: targetUrl,
    });
  };

  useEffect(() => {
    // if (isLogin) {
    //   getData();
    // }
    getData();
  }, [isLogin]);

  return (
    <View className="nutui-react-demo">
      <NoTokenInterceptor />
      <View className="index">欢迎使用 NutUI React 开发 Taro 多端项目。</View>
      <View className="index">
        <Button
          type="primary"
          className="btn"
          onClick={() => {
            setCurrentOption({
              ...currentOption,
              series: [
                {
                  data: [150, 230, 224, 218, 135, 147, 260].map((item) =>
                    Math.floor(Math.random() * 100)
                  ),
                  type: "line",
                },
              ],
            });
          }}
        >
          改变 折线数据
        </Button>
      </View>
      <View className="index">
        <Button
          type="primary"
          className="btn"
          onClick={() => {
            Taro.setStorageSync("token", getRandomString());
          }}
        >
          模拟设置 token
        </Button>
      </View>
      <View className="index">
        <Button
          type="primary"
          className="btn"
          onClick={() => {
            toLogin();
          }}
        >
          跳转网页登录 webview
        </Button>
      </View>
      <View className="index">
        <Button
          type="primary"
          className="btn"
          onClick={() => {
            toSync();
          }}
        >
          同步用户信息
        </Button>
      </View>
      <View className="index">
        <Button
          type="primary"
          className="btn"
          onClick={() => {
            goToPage("/pages/applicationRecord/index");
          }}
        >
          跳转 申诉记录
        </Button>
      </View>
      <BaseEchart option={currentOption} />
    </View>
  );
}

export default Index;
